# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：clearTxtBlank.py
#日期：2019-09-09
#备注：多种去除空行的场景
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from stop_words import get_txtWords,get_skipWords,get_alphaWords,get_keywords
from file_deal import w_save_txt

words = get_skipWords()  # 需要过滤包含 跳过字符 的某一行
m_word = '章 '   # 章节空两段

# 1.查找特殊字符的行
def get_stopTxt(old_txt):
    i = 0
    old_file = open(old_txt,'r',encoding= 'utf-8')
    try:
        for line in old_file.readlines():
            i = i +1
            line = line.lstrip()
            for word in get_alphaWords():
                if word in line:
                    print('第' + str(i) +'行数据：',line)
    except:
        pass


# 2.去除空行 line.split()
# line[0:1]：以*开头的行去除，line[-7:-1]：以*结尾的行去除
def clearAllBlank(old_txt,new_txt):
    old_file = open(old_txt,'r',encoding= 'utf-8')
    new_file = open(new_txt,'w',encoding= 'utf-8')
    try:
        for line in old_file.readlines():
            line = line.lstrip()
            #如果旧txt文件某行不是空行，内容存储到新txt文件中
            if line.split() and line[0:1] not in words and line[0:2] not in words and line[0:3] not in words and line[0:4] not in words and line[0:6] not in words \
                    and line[2:4] not in words and line[3:5] not in words and line[4:6] not in words and line[5:7] not in words \
                    and line[-7:-1] not in words:
                #print(len(line[-7:-1]),line[-7:])
                if 'P：' in line:
                    print('将要被分解的行：',line)
                    new_line = line.split('P：')[0]
                    new_file.writelines('　　'+new_line)
                elif 'P:' in line:
                    print('将要被分解的行：', line)
                    new_line = line.split('P:')[0]
                    new_file.writelines('　　'+new_line)
                elif m_word in line:
                    #print('分章节：'+line)
                    new_file.writelines('\n\n'+line)
                else:
                    new_file.writelines('　　'+line)
            elif line.split():
                #pass
                print('被过滤的行：',line)
    finally:
        new_file.close()
        old_file.close()
    print('----->去除多余行数成功！<-------')


# 3.将不必要的文字去掉
def replace_txt(old_txt,new_txt):
    with open (old_txt, "r", encoding='utf-8') as fp:
        txt = fp.read()
        temp = txt
        for word in get_txtWords():
            if word[0] in txt:
                print('需要替换的文字：',word[0])
                words = temp.replace(word[0],word[1])
                temp = words
        w_save_txt(new_txt,temp)
        print('----->替换不必要的文字成功！<-------')


if __name__ == '__main__':
    i = 3
    if i ==1:
        file = 'F:\PythonProject\python\\12044.txt'
        get_stopTxt(file)  # 1.查看有字母的行（为后面替换做准备）
    elif i == 2:
        file = 'F:\PythonProject\python\\12044.txt'  #
        replace_txt(file, 'F:\PythonProject\python\\12044_1.txt')  # 2.替换一些字段
    else:
        file = 'F:\PythonProject\python\\12044_1.txt'
        clearAllBlank(file, 'F:\PythonProject\python\\12044_2.txt')  # 3.去除多余的行数

